//
//  ViewController.m
//  QQZone
//
//  Created by 树清吴 on 2017/10/15.
//  Copyright © 2017年 shuQingWu. All rights reserved.
//

#import "ViewController.h"
#import "DockVw.h"
#import "DDToolView.h"

@interface ViewController ()

@property (nonatomic, strong) DockVw *dockVw;
@property (nonatomic, weak) UIView *lastView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.dockVw];
    

    [self viewWillTransitionToSize:[UIScreen mainScreen].bounds.size withTransitionCoordinator:self.transitionCoordinator];
    
    for (int i = 0; i < 6; i ++) {
        UIViewController *viewC = [[UIViewController alloc] init];
        viewC.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256) / 255.00 green:arc4random_uniform(256) / 255.00 blue:arc4random_uniform(256) / 255.00 alpha:1];
        [self addChildViewController:viewC];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toolViewButtonClickNotification:) name:kDDToolViewButtonClickNotification object:nil];
    
}

- (void)toolViewButtonClickNotification:(NSNotification *)sender{
    
    [self.lastView removeFromSuperview];
    
    NSDictionary *dic = sender.userInfo;
    UIViewController *viewC = self.childViewControllers[[dic[@"tag"] integerValue]];
    [self.view addSubview:viewC.view];
    
    self.lastView = viewC.view;
    
    // 设置添加上来的view的frame
//    viewC.view.frame = CGRectMake(self.dockVw.width, 0, self.view.width - self.dockVw.width, self.view.height);
    
//    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    viewC.view.translatesAutoresizingMaskIntoConstraints = NO;
    
    /// 给控制器的view添加约束方法二
    [self addContraintMethod2:viewC.view];
    
    NSLog(@"%zd", self.view.subviews.count);
}

/// 给控制器的view添加约束方法二
- (void)addContraintMethod2:(UIView *)view{
    
    NSDictionary *metrics = @{
                              @"width": @(self.dockVw.width)
                              };
    NSDictionary *views = @{
                            @"leftView": self.dockVw,
                            @"rightView": view,
                            };
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[leftView]-0-[rightView]-0-|" options:0 metrics:metrics views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[rightView]-0-|" options:0 metrics:metrics views:views]];
    
}

/// 给控制器的view添加约束方法一
- (void)addContraintMethod1:(UIView *)view{
    // 给添加上来的view添加约束
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.dockVw attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    
    self.dockVw.height = size.height;
    if (size.width < size.height) {
        // 竖屏
        self.dockVw.width = 70;
        
    }else{
        self.dockVw.width = 210;
        
    }
    
    
}



#pragma mark -- 懒加载
- (DockVw *)dockVw{
    if (_dockVw == nil) {
        _dockVw = [[DockVw alloc] init];
    }
    return _dockVw;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
